/*
 * Copyright 2017-8-30 13:56:25 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.core.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.admin.core.RoleManager;
import com.lap.scp.admin.dal.RoleDAO;
import com.lap.scp.admin.domain.RoleDO;
import com.lap.scp.admin.domain.RoleResourcesDO;
import com.lap.scp.admin.query.RoleQueryDO;
import com.lap.scp.admin.support.MybatisBatch;

/**
 * <pre>
 * // TODO 注释
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:25
 * @since 1.0
 */
@Service("roleManager")
public class DefaultRoleManager implements RoleManager {

	@Resource
	private RoleDAO roleDAO;

	@Resource
	private MybatisBatch mybatisBatch;

	@Override
	public int deleteById(int roleId) throws ManagerException {
		roleDAO.deleteResourcesById(roleId);
		return roleDAO.deleteById(roleId);
	}

	@Override
	public void insertRole(RoleDO roleDO) throws ManagerException {
		roleDAO.insertRole(roleDO);
	}

	@Override
	public RoleDO queryById(int roleId) throws ManagerException {
		return roleDAO.queryById(roleId);
	}

	@Override
	public List<RoleDO> queryPage(Page page, RoleQueryDO queryDO) throws ManagerException {
		int count = roleDAO.countPage(queryDO);
		if (count <= 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return roleDAO.queryPage(page, queryDO);
		}
	}

	@Override
	public int updateRole(RoleDO roleDO) throws ManagerException {
		return roleDAO.updateRole(roleDO);
	}

	@Override
	public List<Integer> queryResourcesIdById(int roleId) throws ManagerException {
		return roleDAO.queryResourcesIdById(roleId);
	}

	@Override
	public int bindResources(int roleId, List<RoleResourcesDO> list) throws ManagerException {
		int flag = roleDAO.deleteResourcesById(roleId);
		mybatisBatch.insertBatch("com.lap.scp.admin.dal.RoleDAO.insertRoleResources", list, true);
		return flag;
	}

}
